﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicinaMuncii.Entities;
using MedicinaMuncii.BL.Contracts;
using MedicinaMuncii.IoC;

namespace MedicinaMuncii.UI.Mvc.Controllers
{
    public class ExaminationController : BaseController
    {
        IExaminationService examinationService;
        IEmployeeService employeeService;
        IMedicalAdviceService medicalAdviceService;
        IExaminationTypeService examinationTypeService;
        IExaminationRecomandationService examinationRecomandationService;
        IRecomandationService recomandationService;

        #region Constructor
        public ExaminationController()
        {
            examinationService = IoCWorker.Resolve<IExaminationService>();
            employeeService = IoCWorker.Resolve<IEmployeeService>();
            medicalAdviceService = IoCWorker.Resolve<IMedicalAdviceService>();
            examinationTypeService = IoCWorker.Resolve<IExaminationTypeService>();
            recomandationService = IoCWorker.Resolve<IRecomandationService>();
            examinationRecomandationService = IoCWorker.Resolve<IExaminationRecomandationService>();
        }
        #endregion Constructor

        public ActionResult Index()
        {
            IList<Examination> examinations = examinationService.GetAll(new string[] { "employee", "employee.company", "medicalAdvice", "examinationtype", "employee.person" });
            return View(examinations);
        }

        public ActionResult Create()
        {
            ViewData["Employees"] = employeeService.GetAll(new string[] { "person" });
            ViewData["MedicalAdvices"] = medicalAdviceService.GetAll();
            ViewData["ExaminationTypes"] = examinationTypeService.GetAll();
            ViewData["Recomandations"] = recomandationService.GetAll();

            return View();
        }

        [HttpPost]
        public ActionResult Create(Examination examination, FormCollection form)
        {
            try
            {
                examinationService.Add(examination);

                // Save the recomandations
                foreach (string recomandationKey in form.AllKeys.Where(k => k.Contains("Recomandation")))
                {
                    string recomandationFormValue = form[recomandationKey];

                    if (recomandationFormValue.Contains(','))
                    {
                        recomandationFormValue = recomandationFormValue.Substring(0, recomandationFormValue.IndexOf(','));
                    }

                    bool recomandationSelected = false;
                    Boolean.TryParse(recomandationFormValue, out recomandationSelected);

                    if (recomandationSelected)
                    {
                        ExaminationRecomandation examinationRecomandation = new ExaminationRecomandation();
                        examinationRecomandation.RecomandationID = Convert.ToInt32(recomandationKey.Replace("Recomandation", string.Empty));
                        examinationRecomandation.ExaminationID = examination.ID;
                        examinationRecomandationService.Add(examinationRecomandation);
                    }
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            ViewData["Employees"] = employeeService.GetAll(new string[] { "person" });
            ViewData["MedicalAdvices"] = medicalAdviceService.GetAll();
            ViewData["ExaminationTypes"] = examinationTypeService.GetAll();
            ViewData["Recomandations"] = recomandationService.GetAll();
            Examination examination = examinationService.GetByID(e => e.ID == id, new string[] { "examinationrecomandations" });

            return View(examination);
        }

        [HttpPost]
        public ActionResult Edit(Examination examination, FormCollection form)
        {
            try
            {
                examinationService.Update(examination);

                // Save the recomandations
                foreach (ExaminationRecomandation examinationRecomandation in examinationRecomandationService.Find(er => er.ExaminationID == examination.ID))
                {
                    examinationRecomandationService.Delete(examinationRecomandation);
                }

                foreach (string recomandationKey in form.AllKeys.Where(k => k.Contains("Recomandation")))
                {
                    string recomandationFormValue = form[recomandationKey];

                    if (recomandationFormValue.Contains(','))
                    {
                        recomandationFormValue = recomandationFormValue.Substring(0, recomandationFormValue.IndexOf(','));
                    }

                    bool recomandationSelected = false;
                    Boolean.TryParse(recomandationFormValue, out recomandationSelected);

                    if (recomandationSelected)
                    {
                        ExaminationRecomandation examinationRecomandation = new ExaminationRecomandation();
                        examinationRecomandation.RecomandationID = Convert.ToInt32(recomandationKey.Replace("Recomandation", string.Empty));
                        examinationRecomandation.ExaminationID = examination.ID;
                        examinationRecomandationService.Add(examinationRecomandation);
                    }
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            examinationService.Delete(e => e.ID == id);
            return RedirectToAction("Index");
        }
    }
}
